// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   LinkedList.java

package com.hdy.doubleLinkedList;

import java.io.PrintStream;

public class LinkedList
{
    private static class Node
    {

        Object data;
        Node next;
        Node prev;

        private Node()
        {
        }
    }


    public LinkedList()
    {
        length = 0;
        head.next = tail;
        head.prev = null;
        tail.next = null;
        tail.prev = head;
    }

    public int size()
    {
        return length;
    }

    public boolean remove(Object data)
    {
        return remove(indexOf(data));
    }

    public void reverse()
    {
        if(head.next == tail)
            return;
        Node rh = new Node();
        rh.next = tail;
        Node cn;
        for(Node cur = head.next; cur != tail; cur = cn)
        {
            cn = cur.next;
            cur.next = rh.next;
            rh.next.prev = cur;
            cur.prev = rh;
            rh.next = cur;
        }

        rh.next.prev = head;
        head.next = rh.next;
    }

    public Object get(int index)
    {
        if(index < 0 || index >= length)
            return null;
        int pos = -1;
        Node h = head;
        do
        {
            if(h.next == tail)
                break;
            h = h.next;
            pos++;
        } while(index != pos);
        return h.data;
    }

    public int indexOf(Object data)
    {
        if(length == 0 || data == null)
            return -1;
        boolean found = false;
        int pos = -1;
        Node h = head;
        do
        {
            if(h.next == tail)
                break;
            h = h.next;
            pos++;
            if(!data.equals(h.data))
                continue;
            found = true;
            break;
        } while(true);
        return found ? pos : -1;
    }

    public boolean insert(int index, Object data)
    {
        if(index < 0 || index > length || data == null)
        {
            try
            {
                throw new Exception();
            }
            catch(Exception e)
            {
                System.out.println("\u63D2\u5165\u5931\u8D25\uFF0C\u8D85\u51FA\u94FE\u8868\u8303\u56F4\uFF01");
            }
            return false;
        }
        int pos = -1;
        Node h;
        for(h = head; h.next != tail && index - 1 != pos; h = h.next)
            pos++;

        Node originNext = h.next;
        Node item = new Node();
        item.data = data;
        item.next = originNext;
        item.prev = h;
        originNext.prev = item;
        h.next = item;
        length++;
        return true;
    }

    public boolean remove(int index)
    {
        if(length == 0 || index < 0 || index >= length)
        {
            try
            {
                throw new Exception();
            }
            catch(Exception e)
            {
                System.out.println("\u5220\u9664\u5931\u8D25\uFF0C\u8BE5\u5143\u7D20\u4E0D\u5B58\u5728\uFF01");
            }
            return false;
        }
        int pos = -1;
        Node h = head;
        do
        {
            if(h.next == tail)
                break;
            h = h.next;
            pos++;
        } while(index != pos);
        h.prev.next = h.next;
        h.next.prev = h.prev;
        length--;
        return true;
    }

    public void printForEach()
    {
        for(int i = 0; i < length; i++)
            System.out.println(get(i));

    }

    private final Node head = new Node();
    private final Node tail = new Node();
    private int length;
}
